package zdream.pmfield.stage.buff.ailment;

import zdream.pmfield.data.foe.MoveAdditionalEffects;
import zdream.pmfield.stage.buff.foe.FoeStageBuff;
import zdream.pmfield.stage.director.HookType;
import zdream.pmfield.stage.director.Hooks;
import zdream.pmfield.stage.object.FoeActor;

/**
 * 被束缚时不能移动, 但能够使用行动力
 *
 * TODO 看一下要不要和攻击者关联, 比如攻击方是蛇对 AAA 实施束缚, 蛇跑了束缚还在不
 *
 * @author Zdream
 * @date 2023-12-03
 * @since 0.0.1
 */
public class BoundBuff extends FoeStageBuff {
	public static final String CATEGORY = MoveAdditionalEffects.E_BOUND;
	public static final int MIN_DURATION = 1;
	public final BuffSpan span;

	public BoundBuff(FoeActor target) {
		super(target);
		span = new DurationBuffSpan()
				.withMinDuration(MIN_DURATION)
				.withCheckRateFunction(duration -> duration * 40 - 40)
				.withStartTime(now());
	}

	@Override
	public void init() {
		preventSameBuffBeforeFoeReceiveBuff();
		onHook(HookType.BEFORE_FOE_TURN_START)
				.handle(this::preventMove)
				.buildAndSave();
		onHook(HookType.BEFORE_FOE_TURN_START)
				.handle(span::checkIsNeedCured)
				.buildAndSave();
	}

	public void preventMove(Hooks.HookContext rawContext) {
		Hooks.BeforeTurnHookContext context = rawContext.as();
		if (context.foe != target) {
			return;
		}

		context.allowMove = false;
		context.allowMoveRejectBy = this;
	}

	@Override
	public String getCategory() {
		return CATEGORY;
	}

	@Override
	public String toString() {
		return "被束缚";
	}
}
